package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.dao.ChainTypeDAO;
import com.junzhiit.nengji.shop.model.ChainType;
import com.junzhiit.nengji.shop.model.enums.GoodsStatus;
import com.junzhiit.nengji.shop.model.enums.TypeLevel;

@Repository
public class ChainTypeDAOImpl extends AbstractBaseDAO<ChainType> implements ChainTypeDAO{

	@Override
	public List<ChainType> findAll(String chainId) {
		String sql = "select * from `chain_type` where is_delete=? and chain_id=? order by sort desc";

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(chainId);

		return super.queryForList(sql, ChainType.class, param);
	}

	@Override
	public List<ChainType> getAllName(String chainId){
		String sql = "select id,full_name from `chain_type` where is_delete=? and level<>? and chain_id=? order by level asc";

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(TypeLevel.THREE.toIntValue());
		param.setString(chainId);
		
		return super.queryForList(sql, ChainType.class, param);

	}

	@Override
	public List<ChainType> getGoodsType(String chainId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select id,full_name,name from `chain_type` where is_delete=?");

		StatementParameter param = new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNoneBlank(chainId)){
			sql.append(" and chain_id=?");
			param.setString(chainId);
		}
		
		sql.append(" order by full_code asc ");
		
		return super.queryForList(sql.toString(), ChainType.class, param);
	}

	@Override
	public int getTypeExist(ChainType chainType) {

		StatementParameter param = new StatementParameter();

		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) from `chain_type` where is_delete=? and chain_id=?");
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(chainType.getChainId());

		if (StringUtils.isNotBlank(chainType.getName())) {
			sql.append(" and name=?");
			param.setString(chainType.getName());
		}

		if (StringUtils.isNotBlank(chainType.getParentId())) {
			sql.append(" and parent_id=?");
			param.setString(chainType.getParentId());
		}

		return super.queryForInt(sql.toString(), param);
	}

	@Override
	public String getMaxCode(String parentId) {
		StatementParameter param = new StatementParameter();
		StringBuffer sql = new StringBuffer();
		sql.append("select max(code) from chain_type where 1 = 1");
		if (StringUtils.isNotBlank(parentId)) {
			sql.append(" and parent_id = ?");
			param.setString(parentId);
		}
		return super.queryForString(sql.toString(), param);
	}

	@Override
	public String getMaxFullCode(String parentId) {
		StatementParameter param = new StatementParameter();
		StringBuffer sql = new StringBuffer();
		sql.append("select max(full_code) from chain_type where 1 = 1");
		if (StringUtils.isNotBlank(parentId)) {
			sql.append(" and parent_id = ?");
			param.setString(parentId);
		}
		return super.queryForString(sql.toString(), param);
	}

	@Override
	public int getMaxSort(String parentId) {
		StatementParameter param = new StatementParameter();
		StringBuffer sql = new StringBuffer();
		sql.append("select ifnull(max(sort),0)  from chain_type where 1 = 1");
		if (StringUtils.isNotBlank(parentId)) {
			sql.append(" and parent_id = ?");
			param.setString(parentId);
		}
		return super.queryForInt(sql.toString(), param);
	}

	@Override
	public List<ChainType> getChildId(String parentId) {
		String sql = "select id from type where parent_id=?";
		StatementParameter param = new StatementParameter();
		param.setString(parentId);
		return super.queryForList(sql, ChainType.class, param);
	}

	@Override
	public List<ChainType> getChild(String parentId) {
		String sql = "select * from chain_type where parent_id=?";
		StatementParameter param = new StatementParameter();
		param.setString(parentId);
		return super.queryForList(sql, ChainType.class, param);
	}

	@Override
	public List<ChainType> getGoodsAuditType(String shopId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select distinct t.* from `chain_type` t inner join goods g on g.type_id=t.id");
		sql.append(" and g.status =? and t.is_delete=? and g.is_delete=?");
		
		StatementParameter param = new StatementParameter();
		param.setInt(GoodsStatus.AUDIT.toIntStatus());
		param.setInt(IsDelete.NO.toIntValue());
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNoneBlank(shopId)){
			sql.append(" and t.shop_id=?");
			param.setString(shopId);
		}
		
		sql.append(" order by t.full_code asc ");
		return super.queryForList(sql.toString(), ChainType.class, param);
	}


}
